package com.pearl.web.demo.validation;

import jakarta.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author TangDan
 * @version 1.0
 * @since 2023/3/16
 */
@Slf4j
@Validated
@RestController
public class UserController {

    @PostMapping("/save")
    public Object save(@RequestBody @Validated SaveUserParam saveUserParam/*, BindingResult result*/) {
        // 参数校验结果
/*        if (result.hasErrors()) {
            return Objects.requireNonNull(result.getFieldError()).getDefaultMessage();
        }*/
        log.info("业务逻辑....");
        return saveUserParam;
    }

    @GetMapping("/select")
    public Object select(@Validated({Select.class}) SaveUserParam param) {
        return param;
    }

    @GetMapping("/insert")
    public Object insert(@Validated({Select.class, Insert.class}) SaveUserParam param) {
        return param;
    }

    @GetMapping("/name")
    public Object name(@NotEmpty String name) {
        log.info(name);
        return "Success";
    }


}
